package top.jle.wx.proxy.facade.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import top.jle.jpa.basic.exception.BasicCustomException;
import top.jle.wx.proxy.constant.MessageOption;
import top.jle.wx.proxy.domain.message.SendAllMessage;
import top.jle.wx.proxy.domain.message.SendAllNewsMessage;
import top.jle.wx.proxy.domain.message.SendAllTextMessage;
import top.jle.wx.proxy.exception.ExceptionCode;
import top.jle.wx.proxy.facade.MessageFacade;
import top.jle.wx.proxy.feign.ResultCheckUtil;
import top.jle.wx.proxy.feign.msg.MessageAPIFeign;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessageFacadeImpl implements MessageFacade {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageFacadeImpl.class);
    
    @Autowired
    MessageAPIFeign messageAPIFeign;


    @Override
    public void sendAllMsg(MessageOption.MessageType messageType, String content){
        SendAllMessage message = switch (messageType) {
            case text -> new SendAllTextMessage(content);
            case mpnews -> new SendAllNewsMessage(content);
            default -> null;
        };
        if(message!=null){
            message.setMsgtype(messageType);
            System.out.println(JSONObject.toJSONString(message));
            JSONObject result = messageAPIFeign.sendAll((JSONObject) JSON.toJSON(message));
            if (!ResultCheckUtil.check(result)) {
                LOGGER.error(result.toString());
                throw new BasicCustomException(ExceptionCode.SEND_FAIL);
            }
        }else{
            throw new BasicCustomException(ExceptionCode.NOT_SUPPORTED_MESSAGE_TYPE);
        }


    }

}
